package com.atguigu.vod.Controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.VideoUtils;
import com.atguigu.vod.utils.initVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.Path;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    @PostMapping("uploadVod")
    public R uploadVod(MultipartFile file){
        String videoId = vodService.uploadVideo(file);

        return R.ok().data("videoId",videoId);
    }

    //删除
    @DeleteMapping("removeVideo/{id}")
    public R removeVideo(@PathVariable String id){

        vodService.deleteVideo(id);

        return R.ok();
    }

    //获取视频播放凭证
    @GetMapping("getPlayauth/{id}")
    public R getPlayauth(@PathVariable("id") String id){
        try{
            DefaultAcsClient client = initVideo.initVodClient(VideoUtils.ACCESS_KEY_ID, VideoUtils.ACCESS_KEY_SECRET);

            //创建response，requset对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);

            //得到id
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();

            return R.ok().data("playAuth",playAuth);
        }catch (Exception e){
            throw new GuliException(20001,"获取凭证失败");
        }
    }
}
